import http from "../utils/request"
import {PageRequestType, ResponseType, PageResponseType} from "../types/Response";
import {AxiosResponse} from "axios";
import {UserType} from "./UserRequest";

const apiPrefix:string = "/user";

export interface LoginResponseType {
    token:string,
    refreshToken:string,
    user:UserType
}
//  "mail": "1679309912@qq.com",
//  "password": "zqazqazqa1"
export async function login(data: UserType): Promise<AxiosResponse<ResponseType<LoginResponseType>>> {
    return await http.request({
        data: data,
        url: apiPrefix+'/login',
        method: 'post'
    })
}
export async function logout(): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        url: apiPrefix+'/logout',
        method: 'delete'
    })
}

export interface ToRefreshTokenResponseType {
    token:string
    refreshToken:string
    user:UserType
}

export async function toRefreshToken(refreshToken:string): Promise<AxiosResponse<ResponseType<ToRefreshTokenResponseType>>>{
    return await http.request({
        data: {refreshToken},
        url: apiPrefix+'/refreshToken',
        method: 'post'
    })
}

export interface RegisterRequestType{
    mail:string,
    password:string;
    code:string;
}

export async function register(data:RegisterRequestType): Promise<AxiosResponse<ResponseType<null>>>{
    return await http.request({
        data,
        url: apiPrefix+'/register',
        method: 'post'
    })
}

export interface ResetPasswordRequestType{
    mail:string,
    changedPassword:string;
    code:string;
}

export async function resetPassword(data:ResetPasswordRequestType): Promise<AxiosResponse<ResponseType<null>>>{
    return await http.request({
        data,
        url: apiPrefix+'/resetPassword',
        method: 'put'
    })
}
export async function sendForgetPasswordMail(mailToAddress:string): Promise<AxiosResponse<ResponseType<null>>>{
    return await http.request({
        params:{
            mailToAddress
        },
        url: apiPrefix+'/sendForgetMail',
        method: 'get'
    })
}
export async function sendRegisterMail(mailToAddress:string): Promise<AxiosResponse<ResponseType<null>>>{
    return await http.request({
        params:{
            mailToAddress
        },
        url: apiPrefix+'/sendRegisterMail',
        method: 'get'
    })
}
export async function sendBindMail(newMail :string): Promise<AxiosResponse<ResponseType<null>>>{
    return await http.request({
        params:{
            newMail
        },
        url: apiPrefix+'/sendBindMail',
        method: 'get'
    })
}
export async function sendUnbindMail(): Promise<AxiosResponse<ResponseType<null>>>{
    return await http.request({
        url: apiPrefix+'/sendUnbindMail',
        method: 'get'
    })
}
export async function changeBindMail(bindCode:string,newMail:string,unBindCode:string): Promise<AxiosResponse<ResponseType<null>>>{
    return await http.request({
        params:{
            bindCode,
            newMail,
            unBindCode,
        },
        url: apiPrefix+'/changeBindMail',
        method: 'put'
    })
}

export async function getUserInfo(): Promise<AxiosResponse<ResponseType<UserType>>>{
    return await http.request({
        url: apiPrefix+'/userInfo',
        method: 'get'
    })
}

